package com.kanshu.ksgb.fastread.doudou.module.book.presenter;

import android.util.Log;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.BaseMvpPresenter;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.SimpleChapterBean;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.BookService;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.ChapterRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.share.ShareBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.Subject;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wcy on 2017\7\11 0011.
 */

public class BookListPresenter extends BaseMvpPresenter {

    List<Disposable> mDisposables = new ArrayList<>();
    public BookListPresenter(Subject lifeCyclerSubject) {
        super(lifeCyclerSubject);
    }

    Map<String, String> mReadedMap;
    BookService mBookService;
    Map<String, String> mContentIdMap = new HashMap<>();
    /**
     * 通知后台读到第几章了
     * @param params
     */
    public void notifyServerReadedChapter(ChapterRequestParams params) {
        if (mBookService == null) {
            RetrofitHelper helper = RetrofitHelper.getInstance();
            mBookService = helper.createService(BookService.class);
        }
        if (mContentIdMap.get(params.book_id + "@" + params.content_id) != null) {
            return;
        }
        mBookService.notifyServerReadedChapter(params).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response != null) {
                    Log.d("wcy", response.toString());
                    mContentIdMap.put(params.book_id + "@" + params.content_id, "read");
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
    /**
     * 获取章节分享信息
     * @param params
     */
    public void getChapterShareInfo(ChapterRequestParams params, INetCommCallback<ShareBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "no net");
            return;
        }
        if (mBookService == null) {
            RetrofitHelper helper = RetrofitHelper.getInstance();
            mBookService = helper.createService(BookService.class);
        }
        mBookService.getChapterShareInfo(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<ShareBean>(){
                    @Override
                    public void onResponse(BaseResult<ShareBean> shareBeanBaseResult, ShareBean shareBean, Disposable disposable) {
                        callback.onResponse(shareBean);
                    }
                    @Override
                    public void onError(int code, String errDesc) {
                        super.onError(code, errDesc);
                        callback.onError(code, errDesc);
                    }
                });
    }

    /**
     * 获取该书籍所有章节内容
     * @param bookId
     * @param callback
     */
    public void getAllSimpleChapters(String bookId, INetCommCallback<List<SimpleChapterBean>> callback) {
        if (mBookService == null) {
            RetrofitHelper helper = RetrofitHelper.getInstance();
            mBookService = helper.createService(BookService.class);
        }
        mBookService.getAllSimpleChapters(bookId)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SimpleChapterBean>>(){
                    @Override
                    public void onResponse(BaseResult<List<SimpleChapterBean>> shareBeanBaseResult, List<SimpleChapterBean> shareBean, Disposable disposable) {
                        callback.onResponse(shareBean);
                    }
                    @Override
                    public void onError(int code, String errDesc) {
                        super.onError(code, errDesc);
                        callback.onError(code, errDesc);
                    }
                });
    }

    @Override
    public void detachView() {
        Utils.dispose(mDisposables);
        mContentIdMap.clear();
        super.detachView();
    }
}
